package me.firstapp.manage.product;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.shop.JsonCategory;
import me.firstapp.common.module.shop.JsonProduct;
import me.firstapp.common.module.shop.JsonProductSkuRelation;
import me.firstapp.common.module.shop.JsonSku;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.manage.common.BaseManage;
import me.firstapp.spi.shop.CategoryService;
import me.firstapp.spi.shop.ProductService;
import me.firstapp.spi.shop.ProductSkuRelService;
import me.firstapp.spi.shop.SkuService;

@Controller
@RequestMapping(value = "/productManage")
public class ProductManagePage extends BaseManage {

	@Autowired
	private ProductService productService;

	@Autowired
	private SkuService skuService;

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private ProductSkuRelService apiProductSkuRelService;

	@Value("#{configProperties['qiniu.upload.domain']}")
	private String domain;

	@RequestMapping(value = "/productList.html", method = RequestMethod.GET)
	public ModelAndView productListView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		ListObject<JsonCategory> result = categoryService.findCategoryList();
		modelMap.put("categoryList", result.getItems());
		return new ModelAndView("product/productList", modelMap);
	}

	@RequestMapping(value = "/productAdd.html", method = RequestMethod.GET)
	public ModelAndView productAddView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		ListObject<JsonCategory> result = categoryService.findCategoryList();
		modelMap.put("categoryList", result.getItems());
		modelMap.put("domain", domain);
		return new ModelAndView("product/productAdd", modelMap);
	}

	@RequestMapping(value = "/productUpdate.html", method = RequestMethod.GET)
	public ModelAndView productUpdateView(Long productId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		ListObject<JsonCategory> result = categoryService.findCategoryList();
		modelMap.put("categoryList", result.getItems());
		modelMap.put("domain", domain);
		SingleObject<JsonProduct> _result = productService.findProductById(productId);
		modelMap.put("product", _result.getObject());

		JsonProduct product = _result.getObject();
		if (product != null) {
			String imagesStr = product.getImages();

			if (StrUtils.isNotNULL(imagesStr)) {
				if (imagesStr.contains(",")) {
					String[] imagesAttr = imagesStr.split(",");
					for (int i = 0; i < imagesAttr.length; i++) {
						modelMap.put("otherImage" + (i + 1), imagesAttr[i]);
					}
				} else {
					modelMap.put("otherImage1", imagesStr);
				}
			}
		}
		modelMap.put("productId", productId);
		return new ModelAndView("product/productUpdate", modelMap);
	}

	@RequestMapping(value = "/productDetail.html", method = RequestMethod.GET)
	public ModelAndView productDetailView(Long productId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		SingleObject<JsonProduct> result = productService.findProductById(productId);
		modelMap.put("product", result.getObject());
		return new ModelAndView("product/productDetail", modelMap);
	}

	@RequestMapping(value = "/productImage.html", method = RequestMethod.GET)
	public ModelAndView productImageView(Long productId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		SingleObject<JsonProduct> result = productService.findProductById(productId);
		JsonProduct product = result.getObject();
		if (product != null) {
			String imagesStr = product.getImages();

			if (StrUtils.isNotNULL(imagesStr)) {
				List<String> images = new ArrayList<String>();
				if (imagesStr.contains(",")) {
					String[] imagesAttr = imagesStr.split(",");
					for (int i = 0; i < imagesAttr.length; i++) {
						images.add(imagesAttr[i]);
					}
				} else {
					images.add(imagesStr);
				}
				modelMap.put("images", images);
			}
		}
		return new ModelAndView("common/pictureShow", modelMap);
	}

	@RequestMapping(value = "/productSkuRelList.html", method = RequestMethod.GET)
	public ModelAndView productSkuManageView(Long productId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("productId", productId);
		return new ModelAndView("product/productSkuRelList", modelMap);
	}

	@RequestMapping(value = "/productSkuRelAdd.html", method = RequestMethod.GET)
	public ModelAndView productSkuRelAddManageView(Long productId, HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		modelMap.put("productId", productId);
		Map<String, Object> searchParams = new HashMap<>();
		searchParams.put("productId", productId);
		PageObject<JsonSku> result = skuService.findSkuPage(1, 100, searchParams, null, null);
		Collection<JsonSku> skus = result.getPage().getList();
		modelMap.put("skus", skus);
		return new ModelAndView("product/productSkuRelAdd", modelMap);
	}

	@RequestMapping(value = "/productSkuRelUpdate.html", method = RequestMethod.GET)
	public ModelAndView productSkuRelUpdateView(Long productId, Long productSkuRelId, HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		modelMap.put("productId", productId);

		modelMap.put("productSkuRelId", productSkuRelId);
		SingleObject<JsonProductSkuRelation> _result = apiProductSkuRelService
				.findProductSkuRelationById(productSkuRelId);
		JsonProductSkuRelation rel = _result.getObject();
		modelMap.put("productSkuRel", rel);

		modelMap.put("skuName1", rel.getSkuName1());
		String skuName2 = rel.getSkuName2();
		if (StrUtils.isNotNULL(skuName2)) {
			modelMap.put("skuName2", rel.getSkuName2());
		}

		Map<String, Object> searchParams = new HashMap<>();
		searchParams.put("productId", productId);
		PageObject<JsonSku> result = skuService.findSkuPage(1, 100, searchParams, null, null);
		Collection<JsonSku> skus = result.getPage().getList();
		List<JsonSku> skus1 = new ArrayList<>();
		List<JsonSku> skus2 = new ArrayList<>();
		for (JsonSku apiJsonSku : skus) {
			if (!rel.getSkuName1().equals(apiJsonSku.getName())) {
				skus1.add(apiJsonSku);
			}
			if (!rel.getSkuName2().equals(apiJsonSku.getName())) {
				skus2.add(apiJsonSku);
			}
		}
		modelMap.put("skus1", skus1);
		modelMap.put("skus2", skus2);
		return new ModelAndView("product/productSkuRelUpdate", modelMap);
	}
}
